<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AppointView;
use App\Models\AppointViewRuleTitle;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AppointViewController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AppointView(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('startdate');
            $grid->column('enddate');

            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->disableFilterButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AppointView(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('content');
            $show->field('enddate');
            $show->field('startdate');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AppointView('rules'), function (Form $form) {
                $form->text('title')->required();
                $form->editor('content')->disk('oss')->required();

                $form->date('startdate')->required();
            $form->date('enddate')->required();

                $form->multipleSelect("rules",'关联规则')
                    ->options(AppointViewRuleTitle::query()->pluck('title','id'))
                    ->customFormat(function ($v) {
                        if (!$v) return [];

                        // 这一步非常重要，需要把数据库中查出来的二维数组转化成一维数组
                        return array_column($v, 'id');
                    });
        });
    }
}
